package com.charm.watercress.constract.presenter

import com.charm.codes.lifecycle.Live
import com.charm.codes.mvp.presenter.BaseMvpPresenter
import com.charm.information.api.WatercressServiceManager
import com.charm.watercress.api.Constans
import com.charm.watercress.constract.view.BookView
import com.charm.watercress.entity.BookEntity
import com.charm.watercress.entity.FilmEntity
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers

/**
 * Created by tools on 2018/1/11.
 */
class BookPresenter : BaseMvpPresenter<BookView>() {
    fun bookList(page: Int) {
        var kw = "三国"
        WatercressServiceManager.watercressService()!!.bookList(Constans.apikey, kw, 50)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(Live.bindLifecycle(mvpView))
                .filter { t -> t.data != null }
                .map { t -> t.data!! }
                .subscribe(object : Observer<List<BookEntity>> {
                    override fun onError(e: Throwable) {
                    }

                    override fun onSubscribe(d: Disposable) {
                    }

                    override fun onComplete() {
                        mvpView.netComplete()
                    }

                    override fun onNext(t: List<BookEntity>) {
                        mvpView.bookList(t)
                    }


                })
    }
}